package com.yinNuo.collectWool.collectWoolHttp;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yinNuo.collectWool.core.constant.JiuZhangHeadlinesApiConstant;
import com.yinNuo.collectWool.mobileApi.entity.User;
import com.yinNuo.collectWool.utils.HttpUtils;
import com.yinNuo.collectWool.utils.ToolUtil;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: yinNuo
 * create at: 2021/11/28 上午10:00
 * @description: http请求数据工具类
 */
@Component
@NoArgsConstructor
public class JiuZhangHeadlinesHttps {

    @Autowired
    HttpUtils httpUtils;

    @Autowired
    private User user;

    /**
     * 输入手机号

     *
     * @return
     */
    public String inputPhone(String phone) {
        if (ToolUtil.isEmpty(phone)) {
            return "请输入有效手机号";
        }
        String msg = "";
        HttpHeaders headers = new HttpHeaders();
        List<Map<String, Object>> resultMap = new ArrayList<>();
        String responseBody = httpUtils.getMethod(JiuZhangHeadlinesApiConstant.INPUT_PHONE + "?mobile=" + phone, headers, null);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        if (0 == Integer.parseInt(jsonObject.getStr("code"))) {
            msg = jsonObject.getStr("msg");
        } else {
            msg = "失败";
        }
        return msg;
    }

    /**
     * 登录功能
     *
     * @return
     */
    public String login(String phone, String verificationCode) {
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Length", "113");
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("mobile", phone);
        map.add("code", verificationCode);
        map.add("uuid", "");
        map.add("oaid", "");
        map.add("invite", "");
        map.add("openid", "");
        map.add("device", "Redmi%20K20%20Pro");//手机需要调整一下
        map.add("os", "Android%2010");
        map.add("version", "1.8.4");
        String response = httpUtils.postMethod(JiuZhangHeadlinesApiConstant.LOGIN, headers, map);
        JSONObject responseBody = JSONUtil.parseObj(response);
        if (0 == Integer.parseInt(responseBody.get("code").toString())) {
            String data = responseBody.getStr("data");
            JSONObject jsonObject1 = JSONUtil.parseObj(data);
            user.setToken(jsonObject1.getStr("token"));
            return "登录成功";
        }
        return "登录失败";
    }

    /**
     * 获取九章头条阅读文章列表id
     *
     * @return
     */
    public List<String> getReadArticlesResponse() {
        List<String> result = new ArrayList();
        HttpHeaders headers = new HttpHeaders();
        String responseBody = httpUtils.getMethod(JiuZhangHeadlinesApiConstant.READ_ARTICLE_LIST, headers, null);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        if (0 == Integer.parseInt(jsonObject.get("code").toString())) {
            String data = jsonObject.getStr("data");
            JSONObject jsonObject1 = JSONUtil.parseObj(data);
            JSONArray list = JSONUtil.parseArray(jsonObject1.getStr("list"));
            for (int i = 0; i < list.size(); i++) {
                String id = list.getByPath("[" + i + "]." + "id").toString();
                result.add(id);
            }
            JSONArray placement = JSONUtil.parseArray(jsonObject1.getStr("placement"));
            for (int i = 0; i < placement.size(); i++) {
                String id = placement.getByPath("[" + i + "]." + "id").toString();
                result.add(id);
            }
        }
        return result;
    }

    /**
     * 读取文章赚金币 todo 两分钟获取一次 请求太频繁会请求失败。
     * * @param articleId 读取文章的id
     *
     * @param token token
     * @return
     */
    public Map<String, Object> readArticle(String articleId, String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Length", "49");
        Map<String, Object> resultMap = new HashMap<>();
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("token", token);
        map.add("id", articleId);
        String response = httpUtils.postMethod(JiuZhangHeadlinesApiConstant.READ_ARTICLE, headers, map);
        JSONObject responseBody = JSONUtil.parseObj(response);
        if (0 == Integer.parseInt(responseBody.get("code").toString())) {
            //成功
            resultMap.put("code", 200);
            resultMap.put("msg", responseBody.getStr("msg"));
            resultMap.put("coin", JSONUtil.parseObj(responseBody.getStr("data")).getStr("coin"));
        } else {
            //失败
            resultMap.put("code", 500);
            resultMap.put("msg", responseBody.getStr("msg"));
            resultMap.put("coin", "获取金币失败！");
        }
        resultMap.put("time", DateUtil.now());
        return resultMap;
    }

    /**
     * 获取签到列表
     *
     * @return
     */
    public List<Map<String, Object>> signInList(String token) {

        HttpHeaders headers = new HttpHeaders();
        List<Map<String, Object>> resultMap = new ArrayList<>();
        String responseBody = httpUtils.getMethod(JiuZhangHeadlinesApiConstant.SIGNIN_LIST + "?token=" + token, headers, null);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        if (0 == Integer.parseInt(jsonObject.getStr("code"))) {
            String data = jsonObject.getStr("data");
            JSONObject jsonObject1 = JSONUtil.parseObj(data);
//            JSONArray list = JSONUtil.parseArray(jsonObject1.getStr("list"));
//            for (int i = 0; i < list.size(); i++) {
//                Map<String, Object> map = new HashMap<>();
//                map.put("day", list.getByPath("[" + i + "]." + "day").toString());
//                map.put("isSign", list.getByPath("[" + i + "]." + "is_sign").toString());
//                map.put("money", list.getByPath("[" + i + "]." + "money").toString());
//                resultMap.add(map);
//            }
            Map<String, Object> mapStr = new HashMap<>();
            mapStr.put("money", jsonObject1.getStr("money"));
            mapStr.put("today", jsonObject1.getStr("today"));
            mapStr.put("time", DateUtil.now());
            JSONArray tasks = JSONUtil.parseArray(jsonObject1.getStr("tasks"));//今日签到完成的任务（每天不一样）
            for (int i = 0; i < tasks.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("title", tasks.getByPath("[" + i + "]." + "title").toString());//分享任务
                map.put("desc", tasks.getByPath("[" + i + "]." + "desc").toString());//将九章头条分享到3个群聊
                map.put("target", tasks.getByPath("[" + i + "]." + "target").toString());//分享总次数
                map.put("has", tasks.getByPath("[" + i + "]." + "has").toString());//当前已分享次数
                resultMap.add(map);
            }
            resultMap.add(mapStr);
        }
        return resultMap;
    }

    /**
     * 签到功能模块 todo 一共七天，每天签到方式不同(1直接签到，2分享三次群，3分享三次群+看视频一次) 但貌似都有分享三次群操作
     * todo *************任务做完了但还需要点击签到按钮，暂时未做***************
     *
     * @return
     */
    public Map<String, Object> signIn(String token) {
        Map<String, Object> resultMap = new HashMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Length", "112");
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("token", token);
        map.add("source", "index_sign");
        map.add("device", "Redmi%20K20%20Pro");
        map.add("os", "android");
        map.add("system", "Android%2010");
        String responseBody = httpUtils.postMethod(JiuZhangHeadlinesApiConstant.SIGNIN, headers, map);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        if (0 == Integer.parseInt(jsonObject.getStr("code"))) {
            resultMap.put("msg", jsonObject.getStr("msg"));
            resultMap.put("code", 200);
        } else {
            resultMap.put("msg", jsonObject.getStr("msg"));
            resultMap.put("code", 500);
        }
        resultMap.put("time", DateUtil.now());
        return resultMap;
    }

    /**
     * 完成签到任务后执行签到获取金币 todo
     * @return
     */
//    public Map<String, Object> signIt(){
//
//    }

    /**
     * 分享文章任务 每天可分享五次
     *
     * @param id    文章id
     * @param token
     * @return
     */
    public Map<String, Object> forwardContent(String id, String token) {
        Map<String, Object> resultMap = new HashMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Length", "120");
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("token", token);
        map.add("source", "article");
        map.add("device", "Redmi%20K20%20Pro");
        map.add("os", "android");
        map.add("system", "Android%2010");
        map.add("id", id);
        String responseBody = httpUtils.postMethod(JiuZhangHeadlinesApiConstant.SIGNIN, headers, map);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        if (0 == Integer.parseInt(jsonObject.getStr("code"))) {
            resultMap.put("msg", jsonObject.getStr("msg"));
            resultMap.put("code", 200);
        } else {
            resultMap.put("msg", jsonObject.getStr("msg"));
            resultMap.put("code", 500);
        }
        resultMap.put("time", DateUtil.now());
        return resultMap;
    }

    /**
     * 获取任务页信息
     *
     * @param token
     * @return
     */
    public Map<String, Object> forwardContent(String token) {
        Map<String, Object> resultMap = new HashMap<>();
        HttpHeaders headers = new HttpHeaders();
        String responseBody = httpUtils.getMethod(JiuZhangHeadlinesApiConstant.TASK_INFORMATION + "?token=" + token, headers, null);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        if (0 == Integer.parseInt(jsonObject.getStr("code"))) {
            String data = jsonObject.getStr("data");
            JSONObject jsonObject1 = JSONUtil.parseObj(data);
            resultMap.put("todayCoin", jsonObject1.getStr("integral"));//获取今日金币总数
            resultMap.put("todayMoney", jsonObject1.getStr("assess_money"));//获取今日金币能兑换到的钱
            //新手任务
            JSONArray newTask = JSONUtil.parseArray(jsonObject1.getStr("new_task"));
            for (int i = 0; i < newTask.size(); i++) {
                String name = newTask.getByPath("[" + i + "]." + "name").toString();
                switch (name) {
                    case "新人首次阅读":
                        resultMap.put("newcomerReading", Integer.parseInt(newTask.getByPath("[" + i + "]." + "is_finish").toString()) == 0 ? "未完成" : "已完成");//当前任务是否完成
                        break;
                    case "邀请好友":
                        resultMap.put("inviteFriends", Integer.parseInt(newTask.getByPath("[" + i + "]." + "is_finish").toString()) == 0 ? "未完成" : "已完成");//当前任务是否完成
                        break;
                    case "完善资料":
                        resultMap.put("perfectData", Integer.parseInt(newTask.getByPath("[" + i + "]." + "is_finish").toString()) == 0 ? "未完成" : "已完成");//当前任务是否完成
                        break;
                }
            }
            //日常任务
            JSONArray dailyTask = JSONUtil.parseArray(jsonObject1.getStr("daily_task"));
            for (int i = 0; i < dailyTask.size(); i++) {
                String name = dailyTask.getByPath("[" + i + "]." + "name").toString();
                switch (name) {
                    case "阅读得最大奖励":
                        resultMap.put("readMax", Integer.parseInt(dailyTask.getByPath("[" + i + "]." + "is_finish").toString()) == 0 ? "未完成" : "已完成");//当前任务是否完成
                        break;
                    case "邀请好友阅读":
                        resultMap.put("inviteFriendsRead", Integer.parseInt(dailyTask.getByPath("[" + i + "]." + "is_finish").toString()) == 0 ? "未完成" : "已完成");//当前任务是否完成
                        break;
                    case "签到奖励":
                        resultMap.put("checkInReward", Integer.parseInt(dailyTask.getByPath("[" + i + "]." + "is_finish").toString()) == 0 ? "未完成" : "已完成");//当前任务是否完成
                        break;
                    case "阅读文章":
                        resultMap.put("readArticle", Integer.parseInt(dailyTask.getByPath("[" + i + "]." + "is_finish").toString()) == 0 ? "未完成" : "已完成");//当前任务是否完成
                        break;
                    case "转发文章":
                        resultMap.put("forwardArticle", Integer.parseInt(dailyTask.getByPath("[" + i + "]." + "is_finish").toString()) == 0 ? "未完成" : "已完成");//当前任务是否完成
                        break;
                    case "阅读5分钟":
                        resultMap.put("read5", Integer.parseInt(dailyTask.getByPath("[" + i + "]." + "is_finish").toString()) == 0 ? "未完成" : "已完成");//当前任务是否完成
                        break;
                    case "阅读60分钟":
                        resultMap.put("read60", Integer.parseInt(dailyTask.getByPath("[" + i + "]." + "is_finish").toString()) == 0 ? "未完成" : "已完成");//当前任务是否完成
                        break;
                }
            }
            //顶部每日签到看视频领金币任务
            JSONArray adsTask = JSONUtil.parseArray(jsonObject1.getStr("ads_task"));
            for (int i = 0; i < adsTask.size(); i++) {
                resultMap.put("readads" + i, Integer.parseInt(adsTask.getByPath("[" + i + "]." + "is_finish").toString()) == 0 ? "未完成" : "已完成");//当前任务是否完成
                resultMap.put("adsTask" + i, adsTask.getByPath("[" + i + "]." + "id").toString());//获取当前视频广告id
            }
        }
        resultMap.put("time", DateUtil.now());
        return resultMap;
    }

    /**
     * 顶部四个看视频任务 todo 分次做完任务，待冷却30分钟后再分次看视频
     *
     * @param token
     * @param id    视频id todo  8（30分钟冷却）、9、10、11、   98这是最顶部额外的看视频任务（40分钟冷却）
     * @return
     */
    public Map<String, Object> topAds(String token, String id) {
        Map<String, Object> resultMap = new HashMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Length", "43");
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("token", token);
        map.add("id", id);
        String responseBody = httpUtils.postMethod(JiuZhangHeadlinesApiConstant.TASK_TOP_ADS, headers, map);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        if (0 == Integer.parseInt(jsonObject.getStr("code"))) {
            String data = jsonObject.getStr("data");
            JSONObject jsonObject1 = JSONUtil.parseObj(data);
            resultMap.put("coin", jsonObject1.getStr("coin"));//本次获得的金币
            resultMap.put("msg", jsonObject1.getStr("msg"));
            resultMap.put("code", 200);
        } else {
            resultMap.put("msg", jsonObject.getStr("msg"));
            resultMap.put("code", 500);
        }
        resultMap.put("time", DateUtil.now());
        return resultMap;
    }

    /**
     * 最顶部看视频任务点一下获得的金币，和点看广告翻三倍不冲突
     * 调试显示冷却一个半小时 这里半小时调用一次
     *
     * @param token
     * @return
     */
    public Map<String, Object> extraAds(String token) {
        Map<String, Object> resultMap = new HashMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Length", "38");
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("token", token);
        String responseBody = httpUtils.postMethod(JiuZhangHeadlinesApiConstant.TASK_TOP_ADS_EXTRA, headers, map);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        if (0 == Integer.parseInt(jsonObject.getStr("code"))) {
            String data = jsonObject.getStr("data");
            JSONObject jsonObject1 = JSONUtil.parseObj(data);
            resultMap.put("coin", jsonObject1.getStr("coin"));//本次获得的金币
            resultMap.put("msg", jsonObject1.getStr("msg"));
            resultMap.put("code", 200);
        } else {
            resultMap.put("msg", jsonObject.getStr("msg"));
            resultMap.put("code", 500);
        }
        resultMap.put("time", DateUtil.now());
        return resultMap;
    }

    /**
     * 获取用户信息详情
     *
     * @param token
     * @return
     */
    public Map<String, Object> getPersionalMessage(String token) {
        Map<String, Object> resultMap = new HashMap<>();
        HttpHeaders headers = new HttpHeaders();
        String responseBody = httpUtils.getMethod(JiuZhangHeadlinesApiConstant.USER_INFO + "?token=" + token, headers, null);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        if (0 == Integer.parseInt(jsonObject.get("code").toString())) {
            String data = jsonObject.getStr("data");
            JSONObject jsonObject1 = JSONUtil.parseObj(data);
            resultMap.put("id", jsonObject1.getStr("id"));//用户id
            resultMap.put("userName", jsonObject1.getStr("name"));//用户名
            resultMap.put("mobile", jsonObject1.getStr("mobile"));//手机号
            resultMap.put("sex", Integer.parseInt(jsonObject1.getStr("sex")) == 0 ? "女" : "男");//性别
            resultMap.put("address", jsonObject1.getStr("address"));//地址
            resultMap.put("integral", jsonObject1.getStr("integral"));//今日金币总数
            resultMap.put("money", jsonObject1.getStr("money"));//当前钱包可提现余额
        } else {
            resultMap.put("id", "");//用户id
            resultMap.put("userName", "");//用户名
            resultMap.put("mobile", "");//手机号
            resultMap.put("sex", "");//性别
            resultMap.put("address", "");//地址
            resultMap.put("integral", "");//今日金币总数
            resultMap.put("money", "");//当前钱包可提现余额
        }
        resultMap.put("time", DateUtil.now());
        return resultMap;
    }


    /**
     * 获取用户做的任务信息详情 就是点击金币，查看金币详情的
     * @param token
     * @return
     */
    /*public Map<String, Object> getPersionalTaskDetail(String token){

    }*/


    //以下任务是苹果端


    /**
     * 苹果左上角签到任务
     */
    public Map<String, Object> iphoneLeftTopSign(String token) {
        HttpHeaders headers = new HttpHeaders();
        Map<String, Object> resultMap = new HashMap<>();
        String responseBody = httpUtils.getMethod(JiuZhangHeadlinesApiConstant.IPHONE_LEFT_TOP_SIGN + "?token=" + token, headers, null);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        if (0 == Integer.parseInt(jsonObject.getStr("code"))) {
            String data = jsonObject.getStr("data");
            JSONObject jsonObject1 = JSONUtil.parseObj(data);
            resultMap.put("coin", jsonObject1.getStr("coin"));//本次获得的金币
            resultMap.put("msg", jsonObject1.getStr("msg"));
            resultMap.put("code", 200);
        } else {
            resultMap.put("msg", jsonObject.getStr("msg"));
            resultMap.put("code", 500);
        }
        return resultMap;
    }

    /**
     * 苹果右上角签到后看视频任务
     */
    public Map<String, Object> iphoneRightTopSignAds(String token) {
        Map<String, Object> resultMap = new HashMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Length", "38");
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("token", token);
        String responseBody = httpUtils.postMethod(JiuZhangHeadlinesApiConstant.IPHONE_RIGHT_TOP_SIGN_ADS, headers, map);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        if (0 == Integer.parseInt(jsonObject.getStr("code"))) {
            String data = jsonObject.getStr("data");
            JSONObject jsonObject1 = JSONUtil.parseObj(data);
            resultMap.put("coin", jsonObject1.getStr("coin"));//本次获得的金币
            resultMap.put("msg", jsonObject1.getStr("msg"));
            resultMap.put("code", 200);
        } else {
            resultMap.put("msg", jsonObject.getStr("msg"));
            resultMap.put("code", 500);
        }
        resultMap.put("time", DateUtil.now());
        return resultMap;
    }


    /**
     * 苹果任务栏里的签到
     */
    public Map<String, Object> iphoneSign(String token) {
        Map<String, Object> resultMap = new HashMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Length", "38");
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("token", token);
        String responseBody = httpUtils.postMethod(JiuZhangHeadlinesApiConstant.IPHONE_SIGN, headers, map);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        if (0 == Integer.parseInt(jsonObject.getStr("code"))) {
            String data = jsonObject.getStr("data");
            JSONObject jsonObject1 = JSONUtil.parseObj(data);
            resultMap.put("integral", jsonObject1.getStr("coin"));//本次获得的金币
            resultMap.put("msg", jsonObject1.getStr("msg"));
            resultMap.put("code", 200);
        } else {
            resultMap.put("msg", jsonObject.getStr("msg"));
            resultMap.put("code", 500);
        }
        resultMap.put("time", DateUtil.now());

        //新签到
        MultiValueMap<String, String> map1 = new LinkedMultiValueMap<>();
        map1.add("token", token);
        map1.add("is_double", "1");
        String responseBody1 = httpUtils.postMethod(JiuZhangHeadlinesApiConstant.EVERY_DAY_SIGN, headers, map1);
        return resultMap;
    }

    /**
     * 读文章任务，防止系统检测只赚金币不读文章
     *
     * @param token ?id=2102575&uid=&token=GicEF3I0ZOzkwznfFOqLWTYHc9BqbaER&os=14.2.1&device=iPhone%2012%20Pro%20Max
     * @return
     */
    public Map<String, Object> iphoneReadArticle(String token, String id) {
        Map<String, Object> resultMap = new HashMap<>();
        HttpHeaders headers = new HttpHeaders();
        String responseBody = httpUtils.getMethod(JiuZhangHeadlinesApiConstant.IPHONE_READ_ARTICLE + "?id=" + id + "uid=" + "?token=" + token + "os=14.2.1&device=iPhone%2012%20Pro%20Max", headers, null);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        if (0 == Integer.parseInt(jsonObject.getStr("code"))) {
            String data = jsonObject.getStr("data");
            JSONObject jsonObject1 = JSONUtil.parseObj(data);
            resultMap.put("id", jsonObject1.getStr("id"));//读取文章的id
            resultMap.put("msg", "读取文章成功！");
        } else {
            resultMap.put("id", "无文章id");
            resultMap.put("msg", "读取文章失败！");
        }
        resultMap.put("time", DateUtil.now());
        return resultMap;
    }

    /**
     * 提现任务
     */
    public Map<String, Object> everyDayGetMoney(String token) {
        Map<String, Object> resultMap = new HashMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Length", "38");
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("token", token);
        map.add("type", "1");
        map.add("money", "0.3");
        String responseBody = httpUtils.postMethod(JiuZhangHeadlinesApiConstant.EVERY_DAY_GET_MONEY, headers, map);
        JSONObject jsonObject = JSONUtil.parseObj(responseBody);
        resultMap.put("time", DateUtil.now());
        return resultMap;
    }


}

